﻿using Microsoft.EntityFrameworkCore;
using Abp.Zero.EntityFrameworkCore;
using Partner.Surround.Authorization.Roles;
using Partner.Surround.Authorization.Users;
using Partner.Surround.MultiTenancy;
using Abp.Auditing;
using Abp.EntityHistory;
using Abp.Authorization.Roles;
using Abp.Localization;
using Abp.Configuration;
using Abp.Authorization.Users;

namespace Partner.Surround.EntityFrameworkCore
{
    public class SurroundDbContext : AbpZeroDbContext<Tenant, Role, User, SurroundDbContext>
    {
        #region 基础设施管理
        public virtual DbSet<Partner.Surround.Base.DataDictionaries.DataDictionary> DataDictionary { get; set; }
        public virtual DbSet<Partner.Surround.Base.DataDictionaries.DataDictionaryItem> DataDictionaryItem { get; set; }
        #endregion

        public SurroundDbContext(DbContextOptions<SurroundDbContext> options)
            : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //更改最大长度为255(使用默认256时，mysql部分版本报错)
            modelBuilder.Entity<AuditLog>().Property(a => a.ServiceName).HasMaxLength(255);
            modelBuilder.Entity<AuditLog>().Property(a => a.MethodName).HasMaxLength(255);
            modelBuilder.Entity<EntityChangeSet>().Property(e => e.Reason).HasMaxLength(255);
            modelBuilder.Entity<ApplicationLanguageText>().Property(a => a.Key).HasMaxLength(255);
            modelBuilder.Entity<RoleClaim>().Property(r => r.ClaimType).HasMaxLength(255);
            modelBuilder.Entity<Setting>().Property(s => s.Name).HasMaxLength(255);
            modelBuilder.Entity<UserAccount>().Property(u => u.EmailAddress).HasMaxLength(255);
            modelBuilder.Entity<UserAccount>().Property(u => u.UserName).HasMaxLength(255);
            modelBuilder.Entity<UserClaim>().Property(u => u.ClaimType).HasMaxLength(255);
            modelBuilder.Entity<UserLogin>().Property(u => u.ProviderKey).HasMaxLength(255);
            modelBuilder.Entity<User>().Property(u => u.UserName).HasMaxLength(255);
            modelBuilder.Entity<User>().Property(u => u.EmailAddress).HasMaxLength(255);
            modelBuilder.Entity<User>().Property(u => u.NormalizedEmailAddress).HasMaxLength(255);
            modelBuilder.Entity<User>().Property(u => u.NormalizedUserName).HasMaxLength(255);

            modelBuilder.Entity<User>().Ignore(u => u.Name);
            modelBuilder.Entity<User>().Ignore(u => u.Surname);

        }
    }
}
